<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include '../../include/dbFunctions.php';
include("../../include/class.phpmailer.php");
include ("../../include/image_slider.php");
include ("../../include/function.php");

// Get input from schoolRegistration
$school_id = $_POST['subcat'];
$contact_name = htmlspecialchars($_POST['contact_name'], ENT_QUOTES);
$contact_salutation = $_POST['contact_salutation'];
$contact_email = htmlspecialchars($_POST['contact_email'], ENT_QUOTES);
$mobile_number = $_POST['mobile_number'];
$contact_designation = htmlspecialchars($_POST['contact_designation'], ENT_QUOTES);
$contact_password = htmlspecialchars($_POST['contact_password'], ENT_QUOTES);

$contact_password = sha1($contact_password);

$created_date = date("y-m-j H:i:s ");

if (isset($_POST['office_number'])) {
    $office_number = $_POST['office_number'];
} else {
    $office_number = '';
}

// Perform server validation
$array = array('contact_name', 'contact_salutation', 'contact_email', 'office_number', 'mobile_number', 'contact_designation', 'contact_password');
$empty = check_empty($array);

$mail = check_email('contact_email');

$email = $_POST['contact_email'];

$duplicate_email = check_duplicate('contact_email', 'contact_school', $email);

$activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();

$insert_rep = "INSERT INTO contact_school (school_id, contact_name, contact_salutation, contact_email,   office_number, mobile_number,  contact_designation, contact_password, contact_status)
                                   VALUES ($school_id,'$contact_name', '$contact_salutation', '$contact_email', '$office_number', '$mobile_number', '$contact_designation', '$contact_password', '$activationKey')";
$insert_rep = executeQuery($insert_rep);
if ($_POST['form_submitted'] == '1') {
    //checking input
    if (empty($empty) && empty($mail) && empty($duplicate_emai)) {

        //Can insert school contact?
        if ($insert_rep == 1) {

            $to = $_POST['contact_email'];
            $subject = "JA Registration Activation Email";
            $message = <<<HERE
Welcome to JA Singapore. Thank you for your registration to the JA Scheduling System.

Please click on the link below NOW to active your account:
\rhttp://scheduler.ja.org.sg/school/registration/representativeVerifyRegistration.php?$activationKey\r\r

If you cannot run the link, please copy and paste it to your favourite browser and run.

Thank you for your support of JA Programs.

Best regards,
Ng. Hau Yee (Ms.)
Director
Junior Achievement Singapore


* It is an auto-generated email. Please do not reply
Any question, please email hauyee.ng@ja.org

HERE;
            $headers = "From: hauyee.ng@ja.org" . "\r\n" .
                    "Reply-To: hauyee.ng@ja.org" . "\r\n" .
                    "X-Mailer: PHP/" . phpversion();

            $sendresult = mail($to, $subject, $message, $headers);

           

            if ($sendresult) {

                $message = "An email has been sent to " . $_POST['contact_email'] . " with an activation key. <br/>Please check your mail to complete registration.<br>";
                $message.="<input type='submit' class ='form-back' value='&nbsp' onclick='history.go(-1)'/>";
            } else {
                $message = 'Message was not sent.<br/>';
                $message.= 'Mailer error: ' . $mail->ErrorInfo;
                $message.="<input class ='form-back' type='submit' value='&nbsp' onclick='history.go(-1)'/>";
            }
        } else {
            $message = " An error occur during insert representative account . Please try again! <br>";
            $message.="<input class ='form-back' type='submit' value='&nbsp' onclick='history.go(-1)'/>";
            $message.= $query1;
        }
    } else {
        $message = "Your register is not successful because of these reasons:";
        $message.= $mail . "<br>";
        $message.= $duplicate_email . "<br>";
        for ($i = 0; $i < count($empty); $i++) {
            $message.= $empty[$i] . "<br>";
        }
        $message.="<input type='button' value='&nbsp' onclick='history.go(-1)'/>";
    }
} else {
    ##User isn't registering, check verify code and change activation code to null, status to activated on success

    $queryString = $_SERVER['QUERY_STRING'];

    $result = executeSelectQuery("SELECT * FROM contact_school");

    if ($result) {
        for ($i = 0; $i < count($result); $i++) {
            if ($queryString == $result[$i]["contact_status"]) {

                $id = $result[$i]['contact_school_id'];

                $sql = "UPDATE contact_school SET contact_status='active' WHERE contact_school_id = $id";
                $update = executeQuery($sql);
                $name = "SELECT contact_name FROM contact_school WHERE contact_school_id = $id";
                $name = executeSelectQuery($name);

                if ($update == 1) {

                    $headers = "From: donotreply@jascheduling.com" . "\r\n" .
                            "Reply-To: donotreply@jascheduling.com" . "\r\n" .
                            "X-Mailer: PHP/" . phpversion();


                    $to_admin = "hauyee.ja@gmail.com ";
                    $subject_admin = "New representative registered!";
                    $message_admin = "New representative name: " . $name[0]["contact_name"] . " had successfully registered. Please check your system for more details. ";
                     $message_admin.= "Admin site:http://sit.rp.edu.sg/91199/jascheduling/admin";
                    mail($to_admin, $subject_admin, $message_admin, $headers);


                    $message = "Congratulations!" . $name[0]["contact_name"] . " is now the proud new owner of an JA scheduling System account. ";
                    $message.="Please click to this link to process to main page:
                       <a href = 'http://sit.rp.edu.sg/91199/jascheduling/include/home/homepage.php'> Main Page </a>
                       ";
                }
            }
        }
    }
}
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Junior Achievement Singapore</title>
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>
        <!-- Custom jquery scripts -->
        <script src="../../media/js/jquery/custom_jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <script>
            var timeout    = 500;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="mailto:admin@jasingapore.com">CONTACT</a></li>
                <li><a href="http://jaworldwide.org/">ABOUT JA</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>
            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->

            <div id="featured"><!--FEATURED AREA STARTS-->

                <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"/>

                </div><!--FEATURED IMAGE ENDS-->

                <div class="featured-text"><!--FEATURED TEXT STARTS-->

                    <h2>Welcome to JA Singapore</h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://www.jaworldwide.org/">Read More... </a></p>

                </div><!--FEATURED TEXT ENDS-->

            </div><!--FEATURED AREA ENDS-->

            <?php
            custom_login();
            ?>
            <div id="left-content"><!--RIGHT CONTENT STARTS-->

                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->

                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->

                    <?php
                    echo "<br/><br/><br/><br/><br/><center><p class='pmain'>$message</p></center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    ?>

                </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->

                <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
            </div><!--RIGHT CONTENT ENDS-->
        </div><!--CONTAINER ENDS-->
        <div id="footer"><!--FOOTER STARTS-->
            <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
        </div><!--FOOTER ENDS-->
    </body>
</html>